﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Drawing;

namespace MyCadDll.MyObject
{
    public class MyPoint : MyCadObject, IComparable
    {
        double[] m_p = new double[3];
        double m_x;

        public double X
        {
            get { return m_x; }
            set { m_x = value; }
        }
        double m_y;

        public double Y
        {
            get { return m_y; }
            set { m_y = value; }
        }
        double m_z;

        public double Z
        {
            get { return m_z; }
            set { m_z = value; }
        }
        public double[] P
        {
            get { return m_p; }
            set { m_p = value; }
        }
        public MyPoint()
        {
            for (int i = 0; i < 3; i++)
            {
                X = Y = Z = 0;
                m_p[i] = 0;
            }
        }
        private void SetXYZ()
        {
            X = P[0];
            Y = P[1];
            Z = P[2];
        }
        public MyPoint(double px, double py)
        {
            X = m_p[0] = px;
            Y = m_p[1] = py;
            Z = m_p[2] = 0;
        }
        public MyPoint(double[] ps)
        {
            for (int i = 0; i < 3; i++)
                m_p[i] = ps[i];
            SetXYZ();
        }

        public MyPoint inflate(double dx, double dy)
        {
            return new MyPoint(m_p[0] + dx, m_p[1] + dy);
        }

        public int CompareTo(object obj)
        {
            MyPoint p1;
            if (obj is MyPoint)
            {
                p1 = obj as MyPoint;
                if (Y == p1.Y)
                {
                    if (X == p1.X)
                    {
                        return 0;
                    }
                    else
                    {
                        if (X > p1.X)
                        {
                            return 1;
                        }
                        else
                        {
                            return -1;
                        }
                    }
                }
                else
                {
                    if (Y > p1.Y)
                    {
                        return -1;
                    }
                    else
                    {
                        return 1;
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
            // throw new NotImplementedException();
        }

        public override int CompareTo_X(object obj)
        {
            MyPoint p1;
            if (obj is MyPoint)
            {
                p1 = obj as MyPoint;
                if (X == p1.X)
                {
                    if (Y == p1.Y)
                    {
                        return 0;
                    }
                    else
                    {
                        if (Y > p1.Y)
                        {
                            return -1;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                }
                else
                {
                    if (X > p1.X)
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
            }
            else
            {
                throw new NotImplementedException();

            }
        }

        public override int CompareTo_Y(object obj)
        {
            return CompareTo(obj);
        }

        public static MyPoint operator +(MyPoint p1,MyPoint p2)
        {
            return new MyPoint(p1.X + p2.X,p1.Y + p2.Y);
        }

        public static MyPoint operator +(MyPoint p, double[] d)
        {
            return new MyPoint(p.X + d[0], p.Y + d[1]);
        }
    
    }
}
